package com.android.hedgehog;

import android.util.Log;

public class RisingNumbers {

	public RisingNumber[] choices;
	public boolean[][] playingField = new boolean[320][480];
	int xPosition, yPosition;
	public int p;
	boolean flag;
	int listOrder = 0;
	int difficulty;
	public int totalBoxes;
	double pawn = 0.0;

	public RisingNumbers(int difficulty) {
		this.difficulty = difficulty;
		totalBoxes=5 + (difficulty / 3);
		choices = new RisingNumber[totalBoxes];

		for (int i = 0; i < choices.length; i++) {
			choices[i] = new RisingNumber();
			choices[i].order = i;
		}
		enumerate();
		position();
		display();
	}

	private void enumerate() {
		
		choices[0].firstDigit = 1;
		choices[0].type=0;
		choices[0].evaluate();

		for (int i = 1; i < choices.length; i++) {
			//do{
			pawn= choices[i-1].firstDigit+1;
			p++;
			Log.d("dddd", ""+p+" : "+pawn+ "  <  "+ choices[i-1].value);
			//}while(choices[i-1].value>pawn);
			
			choices[i].order = i;
			if ((Math.random()*1)>.0001) {
				choices[i].firstDigit = i+1;
				choices[i].type=0;
				choices[i].evaluate();
			} else {
				choices[i].firstDigit = (int) pawn;
				choices[i].secondDigit=(int)((pawn-Math.floor(pawn))*10+.5);
				choices[i].type=1;
				choices[i].evaluate();
			}

		}

	}
	private void position() {

		for (int i = 0; i < choices.length; i++) {
			flag = true;

			while (flag) {
				flag = false;
				choices[i].xPosition = (int) (((Math.random() * 250) + 10));
				choices[i].yPosition = (int) (((Math.random() * 360) + 31));

				for (int j = choices[i].xPosition; j < choices[i].xPosition + 40; j++) {
					for (int k = choices[i].yPosition; k < choices[i].yPosition + 40; k++) {
						if (playingField[j][k] == true)
							flag = true;

					}
				}

			}// while(true)

			for (int j = choices[i].xPosition - 10; j < choices[i].xPosition + 50; j++) {
				for (int k = choices[i].yPosition - 10; k < choices[i].yPosition + 50; k++) {
					playingField[j][k] = true;
				}
			}

		}

	}

	private void display() {

	}

	public int checkifHit(boolean[][] XY) {
		for (int i = 0; i < 320; i++) {
			for (int j = 0; j < 480; j++) {
				if (XY[i][j] == true && playingField[i][j] == true) {
					return checkifCorrect(i, j);
				}
			}
		}
		return 0; // nothing hit
	}

	private int checkifCorrect(int a, int b) {
		for (int i = 0; i < choices.length; i++) {
			if ((a - choices[i].xPosition <= 40)
					&& (b - choices[i].yPosition <= 40)
					&& (a - choices[i].xPosition >= 0)
					&& (b - choices[i].yPosition >= 0)) {
				if (choices[i].order == listOrder) {
					listOrder++;
					return 1; // hit correct
				}

			}

		}
		listOrder++;
		return 2; // hit incorrect
	}
}
